#pragma once
// 这里封装一下条件变量 -> 方便后续使用

#include "Mutex.hpp"

using namespace myMutex;

namespace myCond{
    class Cond{
    public:
        Cond(Mutex& mutex)
            :_lock(mutex)
        {
            pthread_cond_init(&_cond, nullptr);
        }

        ~Cond(){
            pthread_cond_destroy(&_cond); 
        }


        void Wait(){
            int wait_id = pthread_cond_wait(&_cond, _lock.GetLockAddr());
            if(wait_id != 0) {std::cout << "condition wait error !" << std::endl;}
        }

        void Signal(){
            int signal_id = pthread_cond_signal(&_cond);
            if(signal_id != 0) {std::cout << "condition siganl error !" << std::endl;}
        }

        void Broadcast(){
            int broadcast_id = pthread_cond_broadcast(&_cond);
            if(broadcast_id) {std::cout << "condition broadcast error !" << std::endl;}
        }

        Mutex& GetMutex(){
            return _lock;
        }

    private:
        pthread_cond_t _cond;
        Mutex& _lock;
    };
}